package com.persistence.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.model.Feriado;
import com.persistence.dao.DAOException;
import com.persistence.dao.DAOManagerMain;
import com.persistence.dao.interfaces.DAOFeriado;

public class MySQLDAOFeriado implements DAOFeriado {
	
	private Feriado feriado = null;
	
	
	public MySQLDAOFeriado() {
	}

	public int insert(Feriado feriado) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("insert into Feriado ("
					+ "Id_Feriado, mes, dia , concepto "
					+ ") " + "values (?, ?, ?, ?) ");

			stmt.setLong(1, feriado.getNroFeriado());
			stmt.setLong(2, feriado.getMes());
			stmt.setLong(3, feriado.getDia());
			stmt.setString(4, feriado.getConcepto());
			stmt.executeUpdate();
			
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);

		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int update(Feriado feriado) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("update Feriado " 
							+ "set mes = ?, dia = ?, concepto = ?"						
							+ " where id_Feriado = ? ");

			stmt.setLong(1, feriado.getMes());
         stmt.setLong(2, feriado.getDia());
         stmt.setString(3, feriado.getConcepto());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int eliminate(Feriado feriado) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("delete from Feriado where id_Feriado = ? ");
			stmt.setLong(1, feriado.getNroFeriado());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public ArrayList<Feriado> select() throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ArrayList<Feriado> feriados = new ArrayList<Feriado>();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("select * from Feriado");
			rs = stmt.executeQuery();

			while (rs.next()) {
				Feriado feriado = new Feriado();
				feriado.setNroFeriado(rs.getInt("Id_Feriado"));
				feriado.setMes(rs.getInt("mes"));
				feriado.setDia(rs.getInt("dia"));
				feriado.setConcepto(rs.getString("Concepto"));

				feriados.add(feriado);
			}

			return feriados;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public Feriado findById(int id) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		feriado = new Feriado();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from Feriado where Id_Feriado = ?");
			stmt.setLong(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
			   feriado.setNroFeriado(rs.getInt("Id_Feriado"));
            feriado.setMes(rs.getInt("mes"));
            feriado.setDia(rs.getInt("dia"));
            feriado.setConcepto(rs.getString("Concepto"));
			}

			return feriado;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	@Override
	public boolean existInstance(Feriado feriado) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		boolean result = false;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from Feriado where UCASE(mes) = ? and UCASE(dia) = ?");

//			stmt.setString(1, feriado.getNombre().toUpperCase());
//			stmt.setString(2, feriado.getApellido().toUpperCase());
			rs = stmt.executeQuery();

			if (rs.next()) {
				result = true;
			}

			return result;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}
}
